﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;


//using UnityStandardAssets.Characters.FirstPerson;
public class StateMachine : MonoBehaviour
{

    private float startTime;
    private bool isGameStart;
    private float freezeTime;
    public FirstPersonController fpc;
    public bool hadShow;
    
    public GameObject go1;
    public GameObject hide;
    public GameObject fire1;
    public ParticleSystem ps1;
    public Camera chaCamera;
    public Camera camera1;
    public Vector3 pos1;
    public Vector3 pos2;
    public Vector3 tempPos;
    public Vector3 newPos;
    public Tweener tweener;
    public Tweener tweener2;
    // Use this for initialization
    void Start()
    {
        isGameStart = false;
        startTime = Time.time;
        freezeTime = 5f;
        hide = GameObject.Find("HideThings");
        fpc = GameObject.Find("FPSController").GetComponent<FirstPersonController>();
        go1 = hide.transform.Find("Tip").gameObject;
        go1.SetActive(false);
        hadShow = false;
        ps1 = GameObject.Find("爆炸").GetComponent<ParticleSystem>();
        pos1 = GameObject.Find("Position1").transform.position;
        camera1 = hide.transform.Find("Main Camera").GetComponent<Camera>();
        camera1.gameObject.SetActive(false);
        tempPos = camera1.transform.position;
        chaCamera = fpc.gameObject.transform.Find("FirstPersonCharacter").GetComponent<Camera>();
        newPos = tempPos;
        fire1 = hide.transform.Find("fire1").gameObject;
       tweener = camera1.transform.DOLocalMove(new Vector3(pos1.x - hide.transform.position.x, pos1.y - hide.transform.position.y, pos1.z - hide.transform.position.z), 1);
        

        tweener.SetAutoKill(false);//把SetAutoKill自动销毁设置为false
        tweener.Pause();
        
    }

    // Update is called once per frame
    void Update()
    {
        //camera1.transform.position = newPos;
        if (Time.time - startTime > freezeTime && !hadShow)
        {
            
            hadShow = true;
            ps1.Play(true);
            chaCamera.DOShakePosition(1);
            Invoke("changeCamera", 1f);
           
            // go1.SetActive(true);
            
            //camera1.DOShakePosition(1);
            
            
            //chaCamera.gameObject.SetActive(true);
            //camera1.gameObject.SetActive(false);
        }
        
        if(go1.activeSelf && Input.GetKey(KeyCode.C))
        {
            if(GameObject.Find("HideThings/Tip/Text").GetComponent<Text>().text == "按C键蹲下")
            {
                dunxia();
            }
            else
            {
                go1.SetActive(false);
                isGameStart = true;
                fpc.cancelFreezeMove();
                fpc.cancelFreezeRotate();
                fpc.StartGame();
            }
        }
    }

    public bool GameStartState()
    {
        
        return isGameStart;
    }

    public void changeCamera()
    {
        fire1.SetActive(true);
        chaCamera.gameObject.SetActive(false);
        camera1.gameObject.SetActive(true);
        move1();
    }

    public void move1()
    {

        //DOTween.To(() => tempPos, x => tempPos = x, new Vector3(pos1.x-tempPos.x, pos1.y - tempPos.y, pos1.z - tempPos.z), 2);
        //DOTween.To(() => newPos, x => newPos = x, new Vector3(5,5,5), 2);
        tweener.PlayForward();
        Invoke("move1Back", 2f);


    }

    public void move1Back()
    {
        //tweener.SetDelay(2);
        tweener.PlayBackwards();
        //tweener2.PlayForward();
        Invoke("cameraBack", 1f);
        //tweener.OnComplete(cameraBack);
        
        
    }

    public void cameraBack()
    {
        chaCamera.gameObject.SetActive(true);
        camera1.gameObject.SetActive(false);
        tipsToDun();
    }

    public void tipsToDun()
    {
        go1.SetActive(true);
        fpc.freezeMove();
        fpc.freezeRotate();
    }
    public void dunxia()
    {
        go1.SetActive(false);
        hide.transform.Find("Tip/Text").GetComponent<Text>().text = "按C键蹲下";

        Invoke("knee", 3f);
    }

    public void knee()
    {
        go1.SetActive(true);
        hide.transform.Find("Tip/Text").GetComponent<Text>().text = "按C键起立";
        
    }

}
